/*
 * @Author: xiaosihan 
 * @Date: 2021-12-24 09:52:46 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2022-02-15 16:19:40
 */

import { DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, RepeatWrapping, WebGLRenderer } from "three";
import ApngTexture from "three-base/ApngTexture";
import dynamicFloorPNG from "../img/dynamicFloor.png";

/**
 * 动态地板纹理
 */
export class DynamicFloor extends Mesh {

    constructor() {
        super();

        this.init();


    }

    static frame = 0;

    static isInit = false;

    static map = new ApngTexture(dynamicFloorPNG);

    static material = new MeshBasicMaterial({
        map: DynamicFloor.map,
        side: DoubleSide,
        transparent: true
    });

    geometry = new PlaneGeometry(10000, 10000, 1, 1);

    renderOrder = 7;

    init() {
        if (!DynamicFloor.isInit) {
            DynamicFloor.isInit = true;
            DynamicFloor.map.wrapS = DynamicFloor.map.wrapT = RepeatWrapping;
            DynamicFloor.map.repeat.set(10, 10);
        }

        this.material = DynamicFloor.material;
        this.rotateX(Math.PI / -2);
        this.position.set(0, 10, 0);
    }

    onBeforeRender = (renderer: WebGLRenderer) => {

        const frame = renderer.info.render.frame;

        if (DynamicFloor.frame !== frame) {
            DynamicFloor.frame = frame;
            DynamicFloor.map.needsUpdate = true;
        }

    }

}